# -*- coding: utf-8 -*-
"""
Created on Thu Oct 29 11:43:57 2020

@author: Administrator
"""

import requests
import pandas as pd
import matplotlib.pyplot as plt

warter_start_time = '2019' + '%24' + '12' + '%24' + '26' + '%24' + '15' + '%3A' + '00' + '%3A' + '00'
warter_end_time = '2029' + '%24' + '12' + '%24' + '28' + '%24' + '18' + '%3A' + '00' + '%3A' + '00'
weather_start_time = '2019' + '%24' + '12' + '%24' + '26' + '%24' + '15' + '%3A' + '00' + '%3A' + '00'
weather_end_time = '2019' + '%24' + '12' + '%24' + '28' + '%24' + '19' + '%3A' + '00' + '%3A' + '00'


def water_temperature():
    global warter_start_time
    global warter_end_time

    # 这个api 可能不合适，时间设置得了解一下。
    query_control_data = 'http://api.station.heating.lanyueyun.com/v1_0_0/hourDatas?access_token=5f9bc4cff90ee13900000083&_id=5dc0dc39685a490001f8b83a&tag_id=20&start_time=' + warter_start_time + '&end_time=' + warter_end_time + '&level=3'

    # query_control_data='http://114.215.46.56:18816/v1_0_0/hourDatas?access_token=5ba1eeb41bc8da00069e146a&_id=5dc0dc39685a490001f8b83a&tag_id=20&start_time='+start_time+'&end_time='+end_time+'level=3'
    water_records = requests.get(query_control_data)
    water_records.encoding = 'utf-8'

    water_datas = water_records.json()
    # print('data json from sys:\n',water_datas)

    # 生成矩阵
    water_temp_dataframe = pd.DataFrame(water_datas)

    # print(water_temp_dataframe)

    # 删除列
    water_temp_dataframe.drop(['data_unit', 'tag_id'], axis=1, inplace=True)
    # print('删除列')
    # print(water_temp_dataframe)

    # 排序
    water_temp_dataframe.sort_values('create_date')

    # print('二网供温数据 行数：',water_temp_dataframe.shape[0])

    # dataframe.rename(columns={'E':'e','F':'f'},inplace = True) # inplace = True，表示在原始dataframe上修改列名
    water_temp_dataframe.rename(columns={'create_date': 'date', 'data_value': 'water_temp'},
                                inplace=True)  # inplace = True，表示在原始dataframe上修改列名

    return water_temp_dataframe

    # Request URL: http://api.station.heating.lanyueyun.com/v1_0_0/hourDatas?access_token=5f9bc4cff90ee13900000083&_id=5dc0dc39685a490001f8b83a&tag_id=20&start_time=2020%2409%2430%2417%3A31%3A00&end_time=2020%2410%2408%2417%3A31%3A00&level=3


def weather_data():
    global weather_start_time
    global weather_end_time

    query_weather_data = 'http://114.215.46.56:18817/v1_0_0/weatherReport?access_token=5f9a24fa1db334410000006f&company_code=000059000001&start_hour=' + weather_start_time + '&end_hour=' + weather_end_time
    # query_weather_data='http://114.215.46.56:18817/v1_0_0/weatherReport?access_token=5f9a24fa1db334410000006f&company_code=000059000001&start_hour=2020%2410%2428%2400%3A00%3A00&end_hour=2020%2410%2429%2400%3A00%3A00'

    weather_records = requests.get(query_weather_data)
    weather_records.encoding = 'utf-8'
    weather_datas = weather_records.json()

    # print(weather_datas)
    weather_dataframe = pd.DataFrame(weather_datas)

    # print(weather_dataframe)

    # 删除列
    weather_dataframe.drop(['img', 'citycode', 'city', 'cityid', 'winddirect'], axis=1, inplace=True)
    # print('删除列')
    # print(weather_dataframe)

    week_mapping = {'星期一': 1, '星期二': 2, '星期三': 3, '星期四': 4, '星期五': 5, '星期六': 6, '星期日': 7}
    weather_dataframe['week'] = weather_dataframe['week'].map(week_mapping)

    # print(weather_dataframe)

    windpower_mapping = {'1级': 1, '2级': 2, '3级': 3, '4级': 4, '5级': 5, '6级': 6, '7级': 7}
    weather_dataframe['windpower'] = weather_dataframe['windpower'].map(windpower_mapping)
    # print(weather_dataframe)

    weather_mapping = {'晴': 1, '阴': 2, '多云': 3, '雨夹雪': 4, '中雪': 5, '小雪': 6, '雾': 7, '小雨': 8, '中雨': 9, '雷阵雨': 10,
                       '大雨': 11, '阵雨': 12}
    weather_dataframe['weather'] = weather_dataframe['weather'].map(weather_mapping)

    # print(weather_dataframe)

    # 显示不重复值
    # print(weather_dataframe['weather'].unique())

    return weather_dataframe

    # 排序
    # weather_datas.sort_values('create_date')

    # print('二网供温数据 行数：',weather_datas.shape[0])

    # plt.rcParams['font.sans-serif'] = ['SimHei']  #显示中文
    # plt.rcParams['axes.unicode_minus']=False #用来正常显示负号

    # weather_dataframe.plot(x='date', y='temp')
    # weather_dataframe.plot(x='date', y='img')
    # plt.show()


def room_temperature():
    start_time_room = '2019-11-01+00:00:00'
    end_time_room = '2019-12-31+23:59:59'

    # 1号楼1单元的 温度计，2169336 没数据
    list_dataid = ['2166715', '2166803', '2166799', '2166811', '2166823', '2166807', '2166819', '2168062', '2166719']
    dataid = '2166719'

    query_room_temperature = 'http://api.community.heating.lanyueyun.com/v1/datas/getDataHistory?access_token=5f9a3593c3c3393800000017&data_id=' + dataid + '&start_time=' + start_time_room + '&end_time=' + end_time_room

    room_temperature = requests.get(query_room_temperature)
    room_temperature.encoding = 'utf-8'

    room_temperature_data = room_temperature.json()

    room_temperature_data_value = room_temperature_data["result"]["data_value"]

    print(room_temperature_data_value)

    print('\n')
    room_temperature_data_time = room_temperature_data["result"]["data_time"]

    print(room_temperature_data_time)

    temp_frame = pd.DataFrame(
        {'temperature_time': room_temperature_data_time, 'temperature': room_temperature_data_value})

    print(temp_frame.head(60))

    temp_frame[(temp_frame.temperature_time.find(':00:00'))]

    # temp_frame=temp_frame.temperature_time.isin([':00:00'])

    print(temp_frame.head(60))
    # print(room_temperature_data)


if __name__ == '__main__':
    weather_train_data = weather_data()
    print('weather rows', weather_train_data.shape[0])
    print(weather_train_data.iloc[:1])
    print(weather_train_data.iloc[-1:])

    print('\n\n')
    weather_water_temp_data = water_temperature()
    print('water temp rows', weather_water_temp_data.shape[0])
    print(weather_water_temp_data.iloc[:1])
    print(weather_water_temp_data.iloc[-1:])

    # 合并数据
    ssha_traindata = pd.merge(weather_train_data, weather_water_temp_data, how='inner', on='date')

    print(ssha_traindata)

    ssha_traindata.to_csv(path_or_buf='ssha_train.csv')
    print('result data file is :ssha_train.csv')